package com.mad.bakergps;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.madc.bakergps.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.*;
import android.widget.*;

public class BakergpsActivity extends Activity implements OnItemSelectedListener {
    /** Called when the activity is first created. */
	
	String[] items;
	ArrayList<Building> buildings;
	ListView roomList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        roomList= (ListView) findViewById(R.id.ListBuildings);
        try {
			buildings=Building.getAllBuildings(R.raw.buildings, this);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        items=Building.convertToStringArray(buildings);
        Spinner buildingSpin = (Spinner) findViewById(R.id.spinner1);
        buildingSpin.setOnItemSelectedListener(this);

		ArrayAdapter aa = new ArrayAdapter(
				this,
				android.R.layout.simple_spinner_item, 
				items);

		aa.setDropDownViewResource(
		   android.R.layout.simple_spinner_dropdown_item);
		buildingSpin.setAdapter(aa);
		

    }
    
 

	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		ArrayList<Room> rooms=new ArrayList<Room>();
		for(Building i: buildings)
		{
			if (i.getName().equals(items[arg2]))
			{
				rooms=i.getRooms();
				break;
			}
		}
		ArrayList<String> roomNumbers= new ArrayList<String>();
		for(Room i:rooms)
		{
			roomNumbers.add("Room" + i.getNumber());
		}
		 ArrayAdapter<String> adapter
		 
		   = new ArrayAdapter<String>(this,
		 
		        android.R.layout.simple_list_item_1,
		 
		        (String  []) roomNumbers.toArray (new String  [roomNumbers.size ()]));
		 
		   roomList.setAdapter(adapter);
		
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}
}